package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.lxl.bt.R;
import com.lxl.bt.bean.OnlyCode;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.MyUtils;
import com.lxl.bt.util.ToastUtil;
import com.yanzhenjie.sofia.Sofia;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  重置密码
 * on  2020年12月3日20:43:33
 **/
public class ResetActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etPhone)
    EditText etPhone;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.etPasswordAgain)
    EditText etPasswordAgain;
    @BindView(R.id.etCode)
    EditText etCode;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.etYaoQin)
    EditText etYaoQin;
    @BindView(R.id.tvLogin)
    TextView tvLogin;
    @BindView(R.id.tvUserPermiss)
    TextView tvUserPermiss;
    @BindView(R.id.tvAppPermiss)
    TextView tvAppPermiss;
    @BindView(R.id.tvCommit)
    TextView tvCommit;
    @BindView(R.id.ivCheck)
    ImageView ivCheck;
    private Context context;
    ACache aCache;
    public String username;
    boolean show = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("重置密码");
        aCache = ACache.get(this);
        username = getIntent().getStringExtra("username");
//        tvCardno.setText(Prefs.getStringValue(Constant.CARDNO));
    }


    private void commit() {
        if (etPhone.getText().toString().isEmpty()) {
            return ;
        }
        if (etCode.getText().toString().isEmpty()) {
            return ;
        }
        if (etPassword.getText().toString().isEmpty()) {
            return ;
        }
        if (etPasswordAgain.getText().toString().isEmpty()) {
            return ;
        }
        if (!etPassword.getText().toString().equals(etPasswordAgain.getText().toString())) {
            return ;
        }

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("code",etCode.getText().toString());
        jsonObject.addProperty("password",etPassword.getText().toString());
        jsonObject.addProperty("phone",etPhone.getText().toString());
        jsonObject.addProperty("visCode", TextUtils.isEmpty(etYaoQin.getText().toString())?"":etYaoQin.getText().toString());
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .addUser(jsonObject).enqueue(new NetCallback<Result<OnlyCode>>() {
            @Override
            public void onSuccess(Result<OnlyCode> result) {
                Log.w("lxl", ">>>>" + result.isSuccess());
                if (result.isSuccess()) {
                    startActivity(new Intent(ResetActivity.this, LoginActivity.class));
                    finish();
                } else {
                    ToastUtil.showToast(ResetActivity.this, result.getMsg());
                }

            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", ">>>>" + errorStr);
                ToastUtil.showToast(ResetActivity.this, errorStr);
            }

        });
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            if (l > 0)
                tvGetCode.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setClickable(true);
            tvGetCode.setText(getResources().getString(R.string.verification));
            tvGetCode.setTextColor(getResources().getColor(R.color.colorMain));
        }
    };
    //获取验证码 （0：注册，1：重置 密码）
    // {
    //  "macCode": "string",
    //  "phone": "string",
    //  "type": "string"
    //}
    private void getVerificationCode() {
        if (etPhone.getText().toString().isEmpty()) {
            return ;
        }
        tvGetCode.setClickable(false);
        tvGetCode.setTextColor(getResources().getColor(R.color.c69));
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("phone", etPhone.getText().toString());
        jsonObject.addProperty("macCode", 1);
        jsonObject.addProperty("type", 0);
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .getyCode(jsonObject).enqueue(new NetCallback<Result<String>>() {
            @Override
            public void onSuccess(Result<String> result) {
               Log.w("lxl", "onsuccess>>>>" );
                timer.start();

            }

            @Override
            public void netLogin() {
                Log.w("lxl", "netLogis>>>>" );
            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", "error>>>>" );
                tvGetCode.setClickable(true);
                tvGetCode.setText(getResources().getString(R.string.verification));
                tvGetCode.setTextColor(getResources().getColor(R.color.colorMain));
                MyUtils.shortToastMsg(context, errorStr);
            }

        });
    }
    @OnClick({R.id.ivCheck,R.id.rlBack, R.id.tvGetCode, R.id.tvLogin, R.id.tvUserPermiss, R.id.tvAppPermiss, R.id.tvCommit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivCheck:
                break;
            case R.id.rlBack:
                break;
            case R.id.tvGetCode:
                getVerificationCode();
                break;
            case R.id.tvLogin:
                break;
            case R.id.tvUserPermiss:
                //WebviewActivity.intent(this,"APP隐私协议","http://oss.puxkj.com/default/1618389446331.html");
                break;
            case R.id.tvAppPermiss:
                WebviewActivity.intent(this,"","http://oss.puxkj.com/default/1618389446331.html");
                break;
            case R.id.tvCommit:
                commit();
                break;
        }
    }
}
